//index.js
//获取应用实例
var app = getApp();
var com = require('../../common.js');
var util = require('../../utils/util.js');

function getDates(days, todate) {
  var dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
}

function dateLater(dates, later) {
  let dateObj = {};
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let date = new Date(dates);
  date.setDate(date.getDate() + later);
  let day = date.getDay();
  let yearDate = date.getFullYear();
  let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.time = yearDate + '-' + month + '-' + dayFormate;
  dateObj.week = show_day[day];
  return dateObj;
}

Page({
  data: {
    active: 0,
    statusd: 1,
    page: 1,
    orderList: [],
    orderPage: '',
    onid: 0,
    confirmedList: [],
    confirmedPage: '',
    actives: 1,
    all: 0,
    deliver: 0,
    listInfowrite: [],
    localMoadl: false,
    checksCodeNumber: '',
    listInfoPages: ''
  },
  dian() {
    com.outTime(1000, function (res) {
      console.log('1')
    })
  },
  //接单跳转详情
  onReceiptOrder(e) {
    var _this = this
    wx.navigateTo({
      url: '/pages/ReceiptPgae/ReceiptPage?id=' + e.currentTarget.id,
    })
  },
  //选中订单状态
  clickTap(e) {
    console.log(e)
    var _this = this,
      id = e.currentTarget.id
    _this.setData({
      onid: id
    })
    _this.orders(id == 0 ? 'today' : id)

  },
  //获取角标数量
  ordersNum() {
    var _this = this
    com.link(app.link.ordersNum, {}, function (rep) {
      console.log(rep)
      _this.setData({
        all: rep.data.response.all,
        deliver: rep.data.response.deliver
      })
    }, "GET")
  },
  onLoad: function () {
    let time = util.formatDate(new Date());
    let date = getDates(365, time);
    console.log(date[364].time);
    var _this = this
    let url = app.link.appraisal,
      data = {}
    com.link(url, data, function (rep) {
      console.log(rep)
      let i = rep.data.response.response
      wx.setStorageSync('store', i.store)

      if (i.seal) {
        if (!i.complete) {
          wx.showModal({
            title: '温馨提示',
            content: '您暂未补全资料，点击确定去补全',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/store/store',
                })
              }
            }
          })
        }
        return false
      }

      if (i.contract) {
        wx.redirectTo({
          url: '/pages/examine/examine?id=4' + "&supplier=" + i.supplier
        })
        return false
      }
      if (i.store > 0 && i.supplier > 0) {
        wx.redirectTo({
          url: '/pages/examine/examine?id=3'
        })
        return false
      }
      wx.redirectTo({
        url: '/pages/examine/examine?id=1'
      })
      _this.setData({
        cheof: rep.data.response.response.auto_receive_order,
        complete: rep.data.response.response.complete
      })

      wx.setStorageSync('storeName', rep.data.response.response.title)
      wx.removeStorageSync('e')
      _this.writeoffOrder('today')
    }, 'GET')
    setTimeout(() => {
      this.setData({
        statusd: 0
      })
    }, 1000)


    com.link('https://szjrzzwdxje.51240.com/web_system/51240_com_www/system/file/szjrzzwdxje/get/', { q: '100000000' }, function (res) {
      console.log(res)
      var i = res.data;
      var list = i.split('');
      var y = []
      list.map((v, k) => {
        if (k > 40) {
          if (list[k] != '<' && list[k] != '/' && list[k] != 's' && list[k] != 'p' && list[k] != 'a' && list[k] != 'n' && list[k] != '>') {
            y.push(list[k])
          }
        }
      })
      console.log(y)
    }, 'GET')
  },
  onShow: function () {
    var _this = this
    _this.accept_accept()
    if (_this.data.actives == 1) {
      _this.ordersNum()
      _this.writeoffOrder(wx.getStorageSync('e') ? wx.getStorageSync('e') : 'today')
    }
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },
  listOnline(e) {
    var _this = this
    wx.navigateTo({
      url: '../orderInfo/index?id=' + e.currentTarget.id,
    })
  },
  //获取核销订单列表
  writeoffOrder(e) {
    var _this = this
    com.link(app.link.writeoffOrder, {
      tab: e
    }, function (rep) {
      console.log(rep);
      _this.setData({
        listInfowrite: rep.data.data,
        listInfoPages: rep.data.links.next
      })
    }, "Get", function (fail) {
      console.log('连接失败==========')
    })
  },
  //切换‘核销订单’ ‘商品订单’
  orderTab: function (e) {
    var _this = this,
      id = e.currentTarget.id,
      o = e.currentTarget.dataset.index
    if (id == 0) {
      _this.setData({
        active: o,
        onid: o == 1 ? 0 : ''
      })
      if (o == 1) {
        _this.orders('')
        _this.accept_accept()
        _this.ordersNum()
      }
    } else {
      _this.setData({
        actives: o,
      })
      console.log(_this.data.onid)
      if (o == 1) {
        var e = 'today'
      } else if (o == 2) {
        var e = 'three'
      } else if (o == 3) {
        var e = 'month'
      } else if (o == 4) {
        var e = ''
      }
      wx.setStorageSync('e', e)
      _this.writeoffOrder(e)
    }
  },
  checksCode(e) {
    var _this = this
    _this.setData({
      checksCodeNumber: e.detail.value
    })
  },
  hexiao: function () {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    setTimeout(() => {
      var _this = this
      if (!_this.data.checksCodeNumber) {
        wx.showToast({
          title: '请输入正确的镜券码',
          icon: 'none'
        })
        return false;
      }
      com.link(app.link.checksCode, {
        code: _this.data.checksCodeNumber
      }, function (rep) {
        console.log(rep);
        if (rep.data.state) {
          wx.navigateTo({
            url: '../bindingShop/bindingShop?string=' + JSON.stringify(rep.data.response.serviceitem),
          })
        } else {
          wx.showToast({
            title: rep.data.message,
            icon: 'none'
          })
        }
      }, "get")

    }, 200)

  },
  codeList: function (e) {
    let index = e.currentTarget.dataset.index;
    let _this = this
    if (index == 0) {
      wx.navigateTo({
        url: '../orderdeliver/orderdeliver?id=' + e.currentTarget.id,
      })
    } else if (index == 1) {
      wx.navigateTo({
        url: '/pages/logistics/logistics?id=' + e.currentTarget.id,
      })
    } else if (index == 2) {
      wx.navigateTo({
        url: '../logistics/logistics?id=' + e.currentTarget.id,
      })
    }
  },
  //隐藏物流窗口
  doNothing() {
    var _this = this
    _this.setData({
      localMoadl: false
    })
  },
  //获取待确认订单
  accept_accept() {
    var _this = this,
      url = app.link.accept_accept,
      data = {}
    com.link(url, data, function (rep) {
      console.log(rep);
      _this.setData({
        confirmedList: rep.data.data,
        confirmedPage: rep.data.links.next,
        lengthNumber: rep.data.data.length
      })
    }, "GET")
  },
  //获取商品列表
  orders(e) {
    console.log(e)
    var _this = this,
      url = app.link.orders,
      data = {
        page: 1,
        state: e
      }
    com.link(url, data, function (rep) {
      console.log(rep);
      _this.setData({
        orderList: rep.data.data,
        orderPage: rep.data.links.next
      })
    }, "GET")
  },
  //点击加载数据
  showList(e) {
    var _this = this,
      id = e,
      list = _this.data.orderList,
      lists = _this.data.listInfowrite

    if (id == 0) {
      let orderPage = _this.data.listInfoPages
      if (!orderPage) {
        console.log('没有更多数据了')
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none'
        })
      } else {
        com.link(orderPage, {}, function (rep) {
          console.log(rep)
          for (let i in rep.data.data) {
            list.push(rep.data.data[i])
          }
          _this.setData({
            listInfowrite: list,
            listInfoPages: rep.data.links.next
          })
        }, "GET")
      }
    } else if (id == 1) {
      let orderPage = _this.data.orderPage
      if (!orderPage) {
        console.log('没有更多数据了')
      } else {
        com.link(orderPage, {}, function (rep) {
          console.log(rep)
          for (let i in rep.data.data) {
            list.push(rep.data.data[i])
          }
          _this.setData({
            orderList: list,
            orderPage: rep.data.links.next
          })
        }, "GET")
      }
    }
  },
  onReachBottom() {
    var _this = this
    _this.showList(_this.data.active)
  },
  //确认订单
  onOrderSuccess(e) {
    var _this = this,
      id = e.currentTarget.id,
      url = app.link.acceptOrder + id + '.st',
      data = {}
    wx.showModal({
      title: '温馨提示',
      content: '确认接单吗？',
      success(e) {
        if (e.confirm) {
          com.link(url, data, function (rep) {
            console.log(rep)
            if (rep.data.state) {
              wx.showToast({
                title: '接单成功',
                icon: 'success'
              })
              setTimeout(() => {
                _this.accept_accept()
                _this.orders('')
                _this.ordersNum()
              }, 1500)
            } else {
              wx.showToast({
                title: rep.data.message,
                icon: 'none'
              })
            }
          }, "GET")
        }
      }
    })
  },
  //拒绝接单
  hendOder(e) {
    var _this = this
    wx.showModal({
      title: '温馨提示',
      content: '确认拒绝订单吗？',
      success(res) {
        if (res.confirm) {
          com.link(app.link.hendOder + e.currentTarget.id + '.st', {}, function (rep) {
            console.log(rep)
            if (!rep.data.state) {
              wx.showModal({
                title: '温馨提示',
                content: rep.data.message,
              })
              return false;
            }
            wx.showToast({
              title: '操作成功',
              icon: 'success'
            })
            setTimeout(() => {
              _this.accept_accept()
            }, 1500)
          }, 'GET')
        }
      }
    })
  },
  baseswitch(e) {
    console.log(e)
    var _this = this
    com.link(app.link.openOrder + wx.getStorageSync('store') + '.st', {}, function (rep) {
      console.log(rep)
      if (e.detail.value) {
        wx.showToast({
          title: '开启自动接单',
          icon: 'success'
        })
      } else {
        wx.showToast({
          title: '关闭自动接单',
          icon: 'success'
        })
      }
    })
  },
  onHide: function () {
    wx.removeStorageSync('e')
  },
  onUnload() {
    console.log('2')
  }
})